package com.lmpop.trade.ti_personal;

import com.lmpop.frame.QueryList;
import com.lmpop.frame.SysMap;
import com.lmpop.frame.TradeInterf;
import com.lmpop.frame.dao.DbmInterf;
import com.lmpop.frame.exp.ApplicationException;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

public class Ti_personalInfo
  implements TradeInterf
{
  QueryList queryList;

  public Ti_personalInfo()
  {
    this.queryList = new QueryList();
  }

  public SysMap insert(DbmInterf dbmMgr, SysMap map)
  {
    Ti_personal ti_personal = new Ti_personal();
    ti_personal.setCust_id(map.getString("cust_id"));
    if (map.getString("bpm_id").equals("2522"))
    {
      ti_personal.setCust_id(map.getString("user_id"));
    }

    if ((map.getString("nike_name") != null) && (!map.getString("nike_name").equals("")))
      ti_personal.setNike_name(map.getString("nike_name"));
    else
      ti_personal.setNike_name(map.getString("user_name"));
    ti_personal.setPhone(map.getString("phone"));
    ti_personal.setEmail(map.getString("email"));
    ti_personal.setCellphone(map.getString("cellphone"));
    ti_personal.setFax(map.getString("fax"));
    ti_personal.setQq(map.getString("qq"));
    ti_personal.setMsn(map.getString("msn"));
    ti_personal.setPost_code(map.getString("post_code"));
    ti_personal.setAddress(map.getString("address"));
    ti_personal.setSex(map.getString("sex"));
    ti_personal.setBirth(map.getString("birth"));
    ti_personal.setArea_attr(map.getString("area_attr"));
    ti_personal.setMarry(map.getString("marry"));
    ti_personal.setBlood_type(map.getString("blood_type"));
    ti_personal.setCareer(map.getString("career"));
    ti_personal.setJob(map.getString("job"));
    ti_personal.setHobby(map.getString("hobby"));
    ti_personal.setIncome(map.getString("income"));
    ti_personal.setPub_user_id(map.getString("user_id"));
    ti_personal.setRemark(map.getString("remark"));
    ti_personal.setPwd_question(map.getString("pwd_question"));
    ti_personal.setPwd_answer(map.getString("pwd_answer"));
    String result_code = "0";
    try {
      insert(dbmMgr, ti_personal);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void insert(DbmInterf dbmMgr, Ti_personal ti_personal)
    throws ApplicationException
  {
    dbmMgr.update("insertTi_personal", ti_personal);
  }

  public SysMap delete(DbmInterf dbmMgr, SysMap map)
  {
    String pkid_string = map.getString("pkid");
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++) {
          if (pkStr[i].equals("")) continue; delete(dbmMgr, pkStr[i]);
        }
      }
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void delete(DbmInterf dbmMgr, String pkid)
    throws ApplicationException
  {
	  //delete from ti_personal where cust_id=#value#
    dbmMgr.update("deleteTi_personal", pkid);
  }

  public SysMap update(DbmInterf dbmMgr, SysMap map)
  {
    Ti_personal ti_personal = new Ti_personal();
    ti_personal.setCust_id(map.getString("cust_id"));
    ti_personal.setNike_name(map.getString("nike_name"));
    ti_personal.setPhone(map.getString("phone"));
    ti_personal.setEmail(map.getString("email"));
    ti_personal.setCellphone(map.getString("cellphone"));
    ti_personal.setFax(map.getString("fax"));
    ti_personal.setQq(map.getString("qq"));
    ti_personal.setMsn(map.getString("msn"));
    ti_personal.setPost_code(map.getString("post_code"));
    ti_personal.setAddress(map.getString("address"));
    ti_personal.setSex(map.getString("sex"));
    ti_personal.setBirth(map.getString("birth"));
    ti_personal.setArea_attr(map.getString("area_attr"));
    ti_personal.setMarry(map.getString("marry"));
    ti_personal.setBlood_type(map.getString("blood_type"));
    ti_personal.setCareer(map.getString("career"));
    ti_personal.setJob(map.getString("job"));
    ti_personal.setHobby(map.getString("hobby"));
    ti_personal.setIncome(map.getString("income"));
    ti_personal.setIn_date(map.getString("in_date"));
    ti_personal.setPub_user_id(map.getString("pub_user_id"));
    ti_personal.setRemark(map.getString("remark"));
    ti_personal.setPwd_question(map.getString("pwd_question"));
    ti_personal.setPwd_answer(map.getString("pwd_answer"));

    String result_code = "0";
    try {
      update(dbmMgr, ti_personal);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void update(DbmInterf dbmMgr, Ti_personal ti_personal)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_personal", ti_personal);
  }

  public Map updatePhone(DbmInterf dbmMgr, SysMap map)
  {
    Hashtable personal = new Hashtable();
    personal.put("cust_id", map.getString("cust_id"));
    personal.put("cellphone", map.getString("cellphone"));
    String result_code = "0";
    String userrand = map.getString("userrand");
    String randcode = map.getString("randcode");
    try {
      dbmMgr.update("updateTi_personal_phone", personal);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    if (userrand.trim().equals(randcode.trim()))
      map.setString("jumpurl", "/program/member/index/group/group_index.jsp");
    else {
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public List getListByPage(Object obj, int start, int limit)
  {
    Ti_personal ti_personal = (Ti_personal)obj;
    if (start == 0)
      start = 0;
    else {
      start = (start - 1) * limit;
    }
    ti_personal.setStart(Integer.valueOf(start));
    ti_personal.setLimit(Integer.valueOf(limit));
    return this.queryList.getList("getListByTi_personalPage", ti_personal);
  }

  public int getCountByObj(Object obj)
  {
    Ti_personal ti_personal = (Ti_personal)obj;
    List list = this.queryList.getList("getCountByObjTi_personal", obj);
    int count = 0;
    if ((list != null) && (list.size() > 0)) {
      Hashtable newsMap = (Hashtable)list.get(0);
      if ((newsMap.get("count") != null) && (!newsMap.get("count").toString().equals(""))) {
        count = Integer.parseInt(newsMap.get("count").toString());
      }
    }
    return count;
  }

  public List getListByPk(String pk)
  {
	  //select cust_id,nike_name,phone,email,cellphone,fax,qq,msn,post_code,address,sex,birth,area_attr,marry,blood_type,career,job,hobby,income,in_date,pub_user_id,remark from ti_personal where cust_id=#value#
    return this.queryList.getList("getTi_personalByPk", pk);
  }

  public List getPorsonalInfosByPK(String pk)
  {
    return this.queryList.getList("getPorsonalInfosByPK", pk);
  }

  public List getPorsonalInfosById(String pk) {
    return this.queryList.getList("getPersonal", pk);
  }

  public String getPersonalNameByPersonalId(String user_id)
  {
    List custList = this.queryList.getList("get_PersonalName_By_PersonalId", user_id);
    String user_name = "";
    Hashtable custMap = new Hashtable();
    if ((custList != null) && (custList.size() > 0)) custMap = (Hashtable)custList.get(0);
    if (custMap.get("user_name") != null) user_name = custMap.get("user_name").toString();
    return user_name;
  }

  public Boolean checkUserEmail(String email) {
    List list = this.queryList.getList("Ti_personal_checkUserEmail", email);
    if ((list != null) && (list.size() > 0)) {
      return Boolean.valueOf(true);
    }
    return Boolean.valueOf(false);
  }

  public String getNameEmail() {
    Ti_personalInfo info = new Ti_personalInfo();
    String selectItems = "";
    List list = this.queryList.getList("getNameEmails");
    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++) {
        Hashtable map = (Hashtable)list.get(i);
        String email = ""; String cust_name = "";
        if (map.get("email") != null) {
          email = map.get("email").toString();
        }
        if (map.get("cust_name") != null) {
          cust_name = info.getPartLength(map.get("cust_name").toString(), 10);
        }
        selectItems = selectItems + "<option value='" + email + "'>" + cust_name + "|" + email + "</option>";
      }
    }
    return selectItems;
  }
  private String getPartLength(String key, int num) { if (key.equals("")) return "";
    if (key.length() > num) key = key.substring(0, num) + "...";
    return key;
  }
}